package com.spring.controller;

import com.spring.respone.ApiRestResponse;
import com.spring.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class IndexController {

    @Autowired
    private GetCodeService getCodeService ;

    @Autowired
    private IdentificationService identificationService ;

    @Autowired
    private LoginService loginService ;

    @Autowired
    private RegisterService registerService ;

    @RequestMapping(value = {"/login"},method = RequestMethod.POST)
    @ResponseBody
    public ApiRestResponse login(HttpServletRequest request){
        System.out.println(request.getSession().getId());
        return loginService.login(request) ;
    }

    @RequestMapping(value = {"/check"})
    public String loginCheck(HttpServletRequest request){
        String goal = loginService.loginCheck(request);
        System.out.println(request.getSession().getId());
        return goal ;
    }

    @RequestMapping(value = {"/register"})
    @ResponseBody
    public ApiRestResponse register(HttpServletRequest request,HttpServletResponse response){
        System.out.println(request.getSession().getId());
        ApiRestResponse re = registerService.register(request) ;
        System.out.println(request.getSession().getId());
        return re ;
    }

    @RequestMapping(value = {"/getCode"})
    @ResponseBody
    public ApiRestResponse getCode(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getSession().getId());
        ApiRestResponse re = getCodeService.getCode(request) ;
        System.out.println(request.getSession().getId());
        return re ;
    }

    @RequestMapping(value = {"/id"})
    @ResponseBody
    public ApiRestResponse identifi(HttpServletRequest request){
        ApiRestResponse re = identificationService.identification(request) ;
        return re ;
    }

    @CrossOrigin
    @RequestMapping(value = "/checkUserPass")
    @ResponseBody
    public ApiRestResponse checkUserPass(HttpServletRequest request,HttpServletResponse response){
        ApiRestResponse re = registerService.checkUserPass(request);
        System.out.println(re);
        return  re ;
    }

    @RequestMapping(value = {"/checkCode"})
    @ResponseBody
    public ApiRestResponse checkCode(HttpServletRequest request, HttpServletResponse response){
        ApiRestResponse re = registerService.checkCode(request) ;
        System.out.println(re);
        return  re ;
    }


    @RequestMapping(value = {"/forGetPwd"})
    @ResponseBody
    public ApiRestResponse forgetpwd(HttpServletRequest request){
        return  registerService.forgotPwd(request) ;
    }
}